import {ref, Ref, watch} from "vue";
import {LoginInfo, RegParams, reqUserAdd, reqUserDel, reqUserList, reqUserRegister} from "@/api/userApi";
import {CODE_OK} from "@/api/consts";

const useUser = () => {
    const userList = (page: Ref<number>, size: Ref<number>, immediate = false) => {
        const list = ref<LoginInfo[]>([]);
        const total = ref(0);
        const loading = ref(false);
        const refresh = async () => {
            loading.value=true;
            let {data, code} = await reqUserList(page.value, size.value);
            loading.value=false;
            if (code === CODE_OK) {
                list.value = data.list || [];
                total.value = data.total || 0;
            }
        };
        watch([page, size], async () => {
            await refresh();
        }, {
            immediate
        })
        return {
            loading,list,total,refresh
        }
    };
    const userAdd = (params:RegParams)=>{
        return reqUserAdd(params);
    };
    const userDel = (id:number)=>{
        return reqUserDel(id);
    };
    return {
        userDel,userList,userAdd
    }
}

export default useUser;